﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Enums;

namespace WMSSoft.Lib.Basic
{
    /// <summary>
    /// 
    /// </summary>
    public static class LanguageStrings
    {
        private static Dictionary<string, Dictionary<string, string>> m_LanguageStringDictionary = new Dictionary<string, Dictionary<string, string>>();
        private static Dictionary<string, string> m_StringsDictionary = new Dictionary<string, string>();
        private static Dictionary<string, string> m_MenuItemStringsDictionary = new Dictionary<string, string>();
        private static Dictionary<string, string> m_MessageStringsDictionary = new Dictionary<string, string>();
        private static Dictionary<string, string> m_ErrorMessageStringsDictionary = new Dictionary<string, string>();

        #region PublicMethods

        public static void ClearAllStringItems()
        {
            //Clean Dictionary
            m_StringsDictionary.Clear();
            m_MenuItemStringsDictionary.Clear();

        }

        public static void LoadMenuItems(System.Xml.XmlNode StringsNode)
        {
            try
            {
                if (StringsNode != null)
                {
                    //Process all child Nodes
                    foreach (System.Xml.XmlNode child in StringsNode.ChildNodes)
                    {
                        LoadStringsItem(child, m_MenuItemStringsDictionary);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to load Strings Configuration from LanguageFile!", ex);
            }
        }

        /// <summary>
        /// Loads all in GUI used strings from Configuration file
        /// </summary>
        /// <param name="StringsNode">XML Node to load from</param>
        public static void LoadStrings(System.Xml.XmlNode StringsNode)
        {
            try
            {                
                if (StringsNode != null)
                {                    
                    //Process all child Nodes
                    foreach (System.Xml.XmlNode child in StringsNode.ChildNodes)
                    {
                        LoadStringsItem(child, m_StringsDictionary);                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to load Strings Configuration from LanguageFile!", ex);
            }
        }

        /// <summary>
        /// Load a Text from Configuration file
        /// </summary>
        /// <param name="Key">Text to load from</param>
        /// <returns>Text from Configuration file</returns>
        public static string GetText(string Key, EStringTypes Type = EStringTypes.stString)
        {
            try
            {
                if (Type == EStringTypes.stString && m_StringsDictionary.ContainsKey(Key) == true)
                    return m_StringsDictionary[Key];
                else if (Type == EStringTypes.stMenuItem && m_MenuItemStringsDictionary.ContainsKey(Key) == true)
                    return m_MenuItemStringsDictionary[Key];
                else if (Type == EStringTypes.stMessage && m_MessageStringsDictionary.ContainsKey(Key) == true)
                    return m_MessageStringsDictionary[Key];
                else if (Type == EStringTypes.stMessage && m_ErrorMessageStringsDictionary.ContainsKey(Key) == true)
                    return m_ErrorMessageStringsDictionary[Key];
                else
                    return Key;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage("Unable to load MenuConfig from File!", ex, EMessageTypes.Error);
                return Key;
            }
        }

        #endregion

        #region PrivateMethods

        /// <summary>
        /// Load a single String to Strings dictionary
        /// </summary>
        /// <param name="Item">XML Node to load String Item from</param>
        private  static void LoadStringsItem(System.Xml.XmlNode Item, Dictionary<string, string> ItemList)
        {
            try
            {
                if (Item != null && ItemList != null)
                {
                    string sKey = "";

                    sKey = Item.Attributes["Key"].Value;
                    //if adding key doesn't exist in dictionary add Text
                    if (sKey.Length > 0 && ItemList.ContainsKey(sKey) == false) ItemList.Add(sKey, Item.Attributes["Text"].Value);
   
                }
                else throw new Exception("You must give a valid XMl Node and ItemList");
            }
            catch (Exception ex)
            {                
                throw new Exception("Unable to load StringItem Configuration from LanguageFile!", ex);
            }
        }
#endregion

    }
}
